package com.cskaoyan.generator;

import com.cskaoyan.service.UserService;
import com.cskaoyan.service.UserServiceImpl;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author stone
 * @date 2022/03/22 20:19
 */
public class JdkGenerator {

    public static void main(String[] args) {
        System.setProperty("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
        UserService userService = new UserServiceImpl();
        UserService proxy = (UserService) Proxy.newProxyInstance(UserService.class.getClassLoader(), UserServiceImpl.class.getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("委托类执行方法之前");
                Object invoke = method.invoke(userService, args);
                System.out.println("委托类执行方法之后");

                return invoke;
            }
        });
        proxy.sayHello("songge");
    }
}
